package com.vision.config;

import com.vision.interceptors.UserAccessControlInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private UserAccessControlInterceptor userAccessControlInterceptor;

    // 拦截器配置
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userAccessControlInterceptor).excludePathPatterns(
                "/user/sendVerificationCode", // 发送验证码
                "/user/register", // 注册
                "/user/loginByVerificationCode", // 通过验证码登录
                "/user/loginByPassword", // 通过密码登录
                "/user/getUserList", // 获取用户搜索结果
                "/tag/getAllTag", // 获取所有标签
                "/video/getVideoForPageLimitByViewCount", // 获取视频列表
                "/video/getVideoForPageByTagId",// 根据标签获取视频列表
                "/video/addViewCount", // 增加视频播放量
                "/video/getVideoById", // 获取视频详情
                "/comment/getParentComment", // 获取父评论
                "/comment/getSonComment" // 获取子评论

        );
    }

    // 跨域配置
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 允许所有路径
                .allowedOriginPatterns("*") // 允许所有源
                .allowCredentials(false) // 允许请求头携带凭证
                .maxAge(3600) // 预检请求的有效期，单位为秒。有效期内，不会重复发送预检请求
                .allowedMethods("*"); // 允许所有请求方法
    }


}
